from flask import Flask, jsonify, request, make_response, render_template, session
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config.from_pyfile("config.py")

db = SQLAlchemy(app)  # 生成db 链接对象


class Book(db.Model):
    __tablename__ = "books"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(30), unique=True, nullable=False)
    author = db.Column(db.String(20), nullable=True)


class Book_s(db.Model):
    __tablename__ = "book_ss"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(30), unique=True, nullable=False)


@app.route("/init")
def init():
    db.create_all()
    return "新表创建成功"

@app.route("/add")
def add():
    # 单条添加
    # bookname1 = Book_s(name="如果")
    # bookname2 = Book_s(name="你还记得")
    #
    # db.session.add(bookname1)
    # db.session.add(bookname2)
    #
    # db.session.commit()

    # 批量添加
    bookname3 = Book_s(name="那么")
    bookname4 = Book_s(name="请你")
    bookname5 = Book_s(name="永远记得")

    db.session.add_all([bookname3,bookname4,bookname5])
    db.session.commit()

    return "添加成功"

@app.route("/drop")
def drop():
    pass



if __name__ == '__main__':
    app.run()
